(function(document) {
	// 当前项目名称
	var projectName = 'proj2';
	// 获取当前js的域名
	var getHost = function() {
		var js = document.scripts;
		var length = js.length;
		var url = '';
		for (var i = 0; i < length; i++) {
			if (js[i].src.indexOf(projectName) != -1) {
				url = js[i].src; // 当前页面中包含的js的路径
				break;
			}
		}
		var hosts = url.split("//");
		var host1 = hosts[0];
		var hostArr = hosts[1].split("/");
		return host1 + "//" + hostArr[0];
	}
	// 当前域名
	var host = getHost();

	/**
	 * 动态加载js/css,以及加载完成后执行回调函数
	 */
	function loadProp(type, id, src, callback) {
		if (type == 'js') {
			var script = document.createElement('script');
			var head = document.getElementsByTagName('head')[0];
			var loaded;
			script.setAttribute('type', 'text/javascript');
			script.setAttribute('charset', 'UTF-8');
			script.setAttribute('src', src);
			if (id != null && id != '') {
				script.setAttribute('id', id);
			}
			if (typeof callback === 'function') {
				script.onload = script.onreadystatechange = function() {
					if (!loaded
							&& (!script.readyState || /loaded|complete/
									.test(script.readyState))) {
						script.onload = script.onreadystatechange = null;
						loaded = true;
						callback();
					}
				}
			}
			head.appendChild(script);
		} else if (type == 'css') {
			var head = document.getElementsByTagName('head')[0]
					|| document.head || document.documentElement;
			var css = document.createElement('link');
			css.setAttribute('type', 'text/css');
			css.setAttribute('rel', 'stylesheet');
			css.setAttribute('href', src);
			if (typeof callback === 'function') {
				css.onload = css.onreadystatechange = function() {
					if (!loaded
							&& (!css.readyState || /loaded|complete/
									.test(css.readyState))) {
						css.onload = css.onreadystatechange = null;
						loaded = true;
						callback();
					}
				}
			}
			head.appendChild(css);
		} else {
			console.log('loadprop type is error!!');
		}
	}

	/**
	 * 格式化跨域请求方式
	 * 
	 * @param obj
	 *            data:参数(json类型) url:必填，请求路径 type:请求类型(默认post)
	 *            success:必填，正确请求返回内容 error:错误请求返回内容
	 */
	window.channelRequest = function(obj) {
		if (typeof (obj.type) == 'undefined' || obj.type == null
				|| obj.type == '') {
			obj.type = 'POST';
		}
		var request = {
			url : obj.url.toLowerCase().indexOf("http") == 0 ? obj.url : host
					+ obj.url,
			type : obj.type,
			dataType : 'json',
			contentType : "application/json; charset=utf-8",
			xhrFields : {
				withCredentials : true
			},
			crossDomain : true,
			success : function(req) {
				obj.success(req);
			},
			error : function(req) {
				obj.error(req);
			}
		};
		if (typeof (obj.data) != 'undefined' && obj.data != null
				&& obj.data != '') {
			$.extend(request, {
				data : JSON.stringify(obj.data)
			});
		}
		$.ajax(request);
	}
	// js加载完成后处理的内容

	var fnInit = function() {
		// 加载相关js
		if (typeof jQuery == 'undefined') {
			// jQuery 未加载
			loadProp('js', null,
					'http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js');
		}
	}

	/**
	 * 根据ID获取用户名
	 * 
	 * @params params:{id:''}
	 */
	window.getNameById = function(params) {
		new channelRequest({
			data : params,
			url : host + '/' + projectName + '/cors/getNameById',
			success : function(ret) {
				req_suc(ret);
			},
			error : function(ret) {
				console.log(ret);
			}
		});
	}

	/*初始化执行方法*/
	fnInit();

})(document);